package com.xingjibase.api;

import android.content.Context;
import android.os.Handler;

import com.fileutils.SPUtils;
import com.google.gson.Gson;
import com.network.ApiFactory;
import com.xingjibase.bean.LoginBean;
import com.xingjibase.params.*;
import io.reactivex.Observer;
import okhttp3.*;


/**
 * @author Xinyi Kong
 * @version 1.0.0
 * @date 2021-11-29
 * @project ISPOTING Factory Production Test
 * @des Http Request Class
 */
public class Http {
    public static String SUCCESS = "SUCCESS";          //成功
    public static String INTERNAL = "INTERNAL";        //内部错误：磁盘错误、内存错误或网络错误等
    public static String PARAM = "PARAM";              //参数错误：为空、非法等验证不通过
    public static String PERM = "PERM";                //权限错误：用户未登录、没有操作权限等
    public static String CONFIG = "CONFIG";            //配置错误：缺少配置项、配置项非法等

    public static String WRONGPWD = "wrong-pwd";         //登陆密码错误
    public static String WRONGLOCKID = "wrong LockID";
    public static String CAPTCHAHUMAN = "captcha-human"; //登陆失败次数过多，需要验证码

    public static String UNREG = "unreg-mobile";         //手机号未注册
    public static String WORNG = "password-old";         //旧密码错误
    public static String CODE_WORNG = "captcha-mobile";   //手机验证码错误

    /**
     * 判断用户是否存在
     *
     * @param account
     * @param iml
     */
    public static void isExists(String account, Observer<ResponseBody> iml) {
        PartForVerify loginPart = new PartForVerify();
        loginPart.setAccount(account);
        ApiFactory.INSTANCE.rxjava_verifyn(iml, loginPart);
    }

    /**
     * 登陆界面
     * 用户登录
     *
     * @param account
     * @param password
     * @param iml
     */

    public static void login(String account, String password, Observer<ResponseBody> iml) {
        PartForLogin loginParams = new PartForLogin();
        loginParams.setAccount(account);
        loginParams.setPassword(password);
        ApiFactory.INSTANCE.rxjava_login(iml, loginParams);
    }

    /**
     * 地锁列表
     *
     * @param iml
     */

    public static void lockList(Context context, Observer<ResponseBody> iml) {
        ApiFactory.INSTANCE.rxjava_lockList(getSessionID(context), iml);
    }

    /**
     * 设置为默认地锁
     *
     * @param iml
     */

    public static void setDefaultLock(Context context, String latchID, Observer<ResponseBody> iml) {
        DefaultLockParams mParams = new DefaultLockParams();
        mParams.setLatchID(latchID);
        mParams.setS(getSessionID(context));
        ApiFactory.INSTANCE.rxjava_defaultLock(mParams, iml);
    }

    /**
     * 设置地锁名称
     *
     * @param iml
     */
    public static void lockSet(Context context, String latchID, String name, Observer<ResponseBody> iml) {
        PartForSetLock mParams = new PartForSetLock();
        mParams.setLatchID(latchID);
        mParams.setS(getSessionID(context));
        mParams.setName(name);
        ApiFactory.INSTANCE.rxjava_lockSet(mParams, iml);
    }

    /**
     * 获取用户基本信息
     *
     * @param iml
     */
    public static void userInfo(Context context, Observer<ResponseBody> iml) {
        ApiFactory.INSTANCE.rxjava_userInfo(getSessionID(context), iml);
    }

    /**
     * 删除地锁
     *
     * @param iml
     */
    public static void deleteLock(Context context, String latchID, Observer<ResponseBody> iml) {
        DefaultLockParams mParams = new DefaultLockParams();
        mParams.setLatchID(latchID);
        mParams.setS(getSessionID(context));
        ApiFactory.INSTANCE.rxjava_deleteLock(mParams, iml);
    }

    /**
     * 添加地锁
     *
     * @param iml
     */
    public static void addLock(Context context, String snCode, String authCode, Observer<ResponseBody> iml) {
        LockAddParams mParams = new LockAddParams();
        mParams.setLockID(snCode);
        mParams.setBindID(authCode);
        mParams.setS(getSessionID(context));
        ApiFactory.INSTANCE.rxjava_addLock(mParams, iml);
    }

    /**
     * 获取SessionID
     *
     * @return
     */
    private static String getSessionID(Context context) {
        String mSessionID = "";
        String mUserInfo = SPUtils.getStringValue(context, SPUtils.KEY_USER_INFO, "");
        if (mUserInfo != null && !mUserInfo.equals("")) {
            mSessionID = new Gson().fromJson(mUserInfo, LoginBean.class).getSessionID();
        }
        return mSessionID;
    }

    /**
     * 刷新登录状态
     *
     * @param iml
     */
    public static void refreshStatus(Context con, Observer<ResponseBody> iml) {
        String mSessionId = getSessionID(con);
        if (!mSessionId.equals("")) {
            ApiFactory.INSTANCE.rxjava_refresh(mSessionId, iml);
        }
    }

    /**
     * 生产测试界面
     * 根据LockID查询地锁数据
     *
     * @param lockID
     * @param iml
     */

    public static void checkId(Context context, String lockID, Observer<ResponseBody> iml) {
        PartForCheckId checkPart = new PartForCheckId();
        checkPart.setLatchID(lockID);
        checkPart.setS(getSessionID(context));
        ApiFactory.INSTANCE.rxjava_checkId(iml, checkPart);
    }

    /**
     * 获取验证码
     *
     * @param mHandler
     */
    public static void getCaptchaHuman(final Handler mHandler) {
//        OkHttpClient client = new OkHttpClient();
//        FormBody body = new FormBody.Builder()
//                .build();
//        Request request = new Request.Builder().post(body).url(URL_CPCA).build();
//        Call call = client.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                HttpUtils.sendMessageData_Error(mHandler);
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                HttpUtils.sendMessageData_Success(response, mHandler);
//            }
//        });
    }

    /**
     * 登陆界面
     * 输入人机验证码后登录
     *
     * @param account
     * @param password
     * @param mHandler
     */
    public static void loginYZM(String account, String password, String captcha, String secret, final Handler mHandler) {
//        OkHttpClient client = new OkHttpClient();
//        MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
//        requestBody.addFormDataPart("Account", account);
//        requestBody.addFormDataPart("Password", password);
//        requestBody.addFormDataPart("CaptchaInput", captcha);
//        requestBody.addFormDataPart("CaptchaSecret", secret);
//        Request request = new Request.Builder().post(requestBody.build()).url(URL_LOGIN).build();
//
//        client.newCall(request).enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                HttpUtils.sendMessageData_Error(mHandler);
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                Log.v("test", "Server: " + response.body().toString());
//                HttpUtils.sendMessageData_Success(response, mHandler);
//            }
//        });
    }
}
